package com.meizhitech.mzcmsapi.service.impl;

import com.hy.corecode.idgen.WFGIdGenerator;
import com.meizhitech.mzcmsapi.api.param.UserLoginParam;
import com.meizhitech.mzcmsapi.common.ServiceResultEnum;
import com.meizhitech.mzcmsapi.utils.ResultGenerator;
import com.meizhitech.mzcmsapi.entity.User;
import com.meizhitech.mzcmsapi.service.UserService;
import com.meizhitech.mzcmsapi.api.param.RegisterParam;
import com.meizhitech.mzcmsapi.dao.UserMapper;
import com.meizhitech.mzcmsapi.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private WFGIdGenerator wFGIdGenerator;

    public String register(RegisterParam registerParam){
        //判断用户账号是否已经存在
        if(userMapper.selectByAccount(registerParam.getAccount())!=null){
            return ServiceResultEnum.SAME_LOGIN_NAME_EXIST.getResult();
        }
        User registerUser = new User();
        registerUser.setUserId(wFGIdGenerator.next());
        registerUser.setUserName(registerParam.getUserName());
        //密码md5加密
        String passwordMD5 = MD5Util.MD5Encode(registerParam.getPassword(), "UTF-8");
        registerUser.setPassword(passwordMD5);
        registerUser.setAccount(registerParam.getAccount());
        registerUser.setEmail(registerParam.getEmail());
        registerUser.setAddress(registerParam.getAddress());
        registerUser.setPhone(registerParam.getPhone());
        registerUser.setAvatar(registerParam.getAvatar());
        //数据库插入数据，判断是否成功
        if(userMapper.insertSelective(registerUser)>0){
            return  ServiceResultEnum.SUCCESS.getResult();
        }
        return  ServiceResultEnum.DB_ERROR.getResult();
    }

    public String login(UserLoginParam userLoginParam){
        //判断账号是否存在
        User loginUser = userMapper.selectByAccount(userLoginParam.getAccount());
        if(loginUser==null){
            return ServiceResultEnum.ACCOUNT_NOT_FIND.getResult();
        }
        System.out.println(loginUser);
        //判断密码是否相同
        return  ServiceResultEnum.DB_ERROR.getResult();
    }
}
